home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4421 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.0 KB

  1. From: CompuWord@msn.com (Meiyu Lin)
  2. Subject: RE: Coding Standards for C++
  3. Date: 30 Jan 96 03:58:39 -0800
  4. References: <4dhuok$qpl@news3.digex.net>
  5. Message-ID: <00001a81+0000965d@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c++
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. How is the following sounds to you:
  11.  
  12. c) In a similar way, the declaration of a function should be procceeded by a
  13.    comment describing what the function or procedure does, and how it does it.
  14.  
  15. d) Lower-case letters should be used for KEY WORDS and Mix-case letters
  16.    should be used for VARIABLE NAMES. 
  17.  
  18. e) Variable names should be mnemonic of what they are used for.
  19.  
  20. f) #define should be in all CAPITAL letters to emphasiz the fact that the
  21.    symbol is a constant and not a variable.
  22.  
  23. g) Spaces, tabs (set to 4 characters), and blank lines should be freely used.
  24.  
  25. h) The following operators should be surounded by spaces, such as:
  26.    arithmetic binary operators:
  27.    multiplication, division, addition, subtraction, modulus : (*, /, +, -, %).
  28.    assignment (=), equals operator (==), not equal (!=), less than 
  29. (<), less than
  30.    or equal to (<=), grater than or equal to (>=), left shift (<<), 
  31. right shift (>>),
  32.    assignment operators: +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=.
  33.    logic operators: &&, ||
  34.    bit operators: &, |
  35.  
  36.    The following unary operators (--, ++, !, ~) should be precede by a space, 
  37.    commas (,) should be followed by a space.
  38.  
  39.    i)  Multiple statements placed on a single line tend to get hidden 
  40. should be
  41.        avoided.
  42.  
  43.    j) Statements contained in a compound statement should be indented by 4
  44.       spaces.
  45.  
  46.    k) If a statement will not fit on a line, it should be broken up 
  47. as best possible
  48.       put on 2 lines.  The second line should be spaced over so that 
  49. it it longer
  50.       than the first.
  51.  
  52.       Example:
  53.     void TheFunctionName() {int    sum=0, j;   sum=sum+j;printf("This is a
  54.            very bad example never do anything like this");j++;}
  55.  
  56.     void   TheFunctionName()
  57.       {
  58.         int      sum = 0, j = 0;
  59.  
  60.           sum += j;        /*  sum = sum + j  */
  61.           printf ("This is a very long write statement ");
  62.           printf ("and will not fit on a single line...\n");
  63.              j++;
  64.       } 
  65.       
  66. 2. Control statements:
  67. ╖ Statements which are executed as the result of an if, or the 
  68. looping statement: while, do    
  69.   while, for should be indented by 4 spaces.
  70.  
  71. ╖ If the statement to be executed is a compound statement then the { 
  72. and } should be 
  73.   placed on a line by themselves and indented the same amount as the 
  74. loop statement.
  75.  
  76.   void ObjectClassName::BlockExample ()
  77.   {
  78.      char        buffer[80];
  79.      int         i, length;
  80.      int         sum = 0;
  81.  
  82.      /*
  83.      **    This comment style will be used for long comments lines.  It 
  84.      **    explains what are you doing in the following statements.
  85.      */
  86.  
  87.      printf ("Enter up to 75 characters and contains a * symbol:\n\n");
  88.      scanf ("%s", buffer);
  89.                        
  90.      //  Find the length of the input string.   
  91.      length = strlen (buffer);
  92.                    
  93.      /*  Search for the æ*Æ  */
  94.      for (i = 0; i < length; i++)
  95.      {
  96.          if (buffer[i] != '*')
  97.          {
  98.             printf ("%c", buffer[i]);
  99.                ++sum;
  100.           }
  101.           else
  102.           {
  103.                                                                       
  104.                                                                       
  105.                                                                       
  106.                                                                       
  107.                                                                         
  108. /*
  109.                **    Found the "*" print it out and break the for loop
  110.                */
  111.                printf ("\n A start in your input at position %3d.\n", sum+1);
  112.                     break; 
  113.          }
  114.          
  115.          //  User's input doesn't contains a *
  116.          if (i == length)
  117.              printf ("You didnÆt input \"*\" as the string terminater\n");
  118.       }                         
  119.   }
  120.       
  121.